User
<script setup>
import {Delete, Edit} from '@element-plus/icons-vue'

import {ref} from 'vue'
import {courseAddService, courseListService, courseUpdateService, deleteCourseService} from "@/api/course";
import {useTokenStore} from '@/stores/token.js';
import {ElMessage, ElMessageBox} from 'element-plus'
import {teachersListService} from '@/api/teacher.js'

const teachers = ref();
const getteachers = async () => {
  let result = await teachersListService();
  teachers.value = result.data;
}
getteachers()

const courses = ref([
  {
    id: '',
    name: '',
    teacherId: '',
    courseDescription: '',
    teacherName: ''
  }
])

const teacherId = ref('')
const id = ref('')


const pageNum = ref(1)//当前页
const total = ref(0)//总条数
const pageSize = ref(5)//每页条数

const onSizeChange = (size) => {
  pageSize.value = size
  courseList()
}
const onCurrentChange = (num) => {
  pageNum.value = num
  courseList()
}

let pageSizes = [5, 10, 15];


const courseList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    teacherId: teacherId.value ? teacherId.value : null,
    id: id.value ? id.value : null,

  }

  let result = await courseListService(params);
  total.value = result.data.total;
  courses.value = result.data.items;
  for (let i = 0; i < courses.value.length; i++) {
    let course1 = courses.value[i];
    for (let j = 0; j < teachers.value.length; j++) {
      if (course1.teacherId == teachers.value[j].id) {
        course1.teacherName = teachers.value[j].name;
      }
    }
  }
}

courseList()

const courseModel = ref({
  id: '',
  name: '',
  teacherId: '',
  courseDescription: ''
})


const tokenStore = useTokenStore();


const addCourse = async () => {

  let result = await courseAddService(courseModel.value);
  ElMessage.success(result.msg ? result.msg : '添加成功');

  dialogVisible.value = false;

  await courseList()
}


const updateCourse = async () => {

  try {
    const result = await courseUpdateService(courseModel.value);
    ElMessage.success(result.msg ? result.msg : '修改成功');
  } catch (error) {
    ElMessage.error('修改失败：' + error.message);
  }

  dialogVisible.value = false;

  await courseList();
}


const deleteCourse = (row) => {

  ElMessageBox.confirm(
      '你确认要删除该课程吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let result = await deleteCourseService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        await courseList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消删除',
        })
      })
}


const clearData = () => {
  courseModel.value.id = '';
  courseModel.value.name = '';
  courseModel.value.teacherId = '';
  courseModel.value.courseDescription = '';
}

const dialogVisible = ref(false)
const title = ref('')
const showDialog = (row) => {
  dialogVisible.value = true;
  title.value = '编辑课程'
  courseModel.value.name = row.name;
  courseModel.value.teacherId = row.teacherId;
  courseModel.value.courseDescription = row.courseDescription;
  courseModel.value.id = row.id
}

</script>
<template>
  <el-card>
    <template #header>
      <div class="header">
        <div class="extra">
          <el-button type="primary" @click="dialogVisible = true;clearData();title = '添加课程'">添加课程</el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="课程ID：">
        <el-input v-model="id" placeholder="请输入课程ID"></el-input>
      </el-form-item>

      <el-form-item label="教师ID：">
        <el-input v-model="teacherId" placeholder="请输入教师ID"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="courseList()">搜索</el-button>
        <el-button @click="id = '';name = '';teacherId = ''; courseList()">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 课程列表 -->
    <el-table :data="courses" style="width: 100%">
      <el-table-column label="课程ID" prop="id"></el-table-column>
      <el-table-column label="课程名字" prop="name"></el-table-column>
      <el-table-column label="教师" prop="teacherName"></el-table-column>
      <el-table-column label="教师ID" prop="teacherId"></el-table-column>
      <el-table-column label="课程描述" prop="courseDescription"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteCourse(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="pageSizes"
                   :total="total" background layout="jumper, total, sizes, prev, pager, next"
                   style="margin-top: 20px; justify-content: flex-end"
                   @size-change="onSizeChange" @current-change="onCurrentChange"/>

    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="courseModel" label-width="100px" style="padding-right: 30px">
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="courseModel.name" maxlength="10" minlength="1"></el-input>
        </el-form-item>
        <el-form-item label="课程描述" prop="courseDescription">
          <el-input v-model="courseModel.courseDescription" maxlength="15" minlength="1"></el-input>
        </el-form-item>
        <el-form-item label="班级导师" prop="advisor">
          <el-select v-model="courseModel.teacherId" placeholder="请选择">
            <el-option v-for="t in teachers" :key="t.id" :label="t.name" :value="t.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary"
                               @click="title == '添加课程' ? addCourse() : updateCourse()"> 确认 </el-button>
                </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

</style>